package com.burt.springcloudconfigclientdemo.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.env.EnvironmentEndpoint;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * Description: User: Burt Date: 2018-07-15 Time: 11:31
 */
@RestController
@RefreshScope
public class EchoEnvController {

    @Value("${wechat.appId}")
    private String                    wechatAppId;

    private final Environment         environment;

    private final EnvironmentEndpoint environmentEndpoint;

    @Autowired
    public EchoEnvController(Environment environment, EnvironmentEndpoint environmentEndpoint) {
        this.environment = environment;
        this.environmentEndpoint = environmentEndpoint;
    }

    @GetMapping("/echo/env/{name}")
    public Map<String, Object> environment(@PathVariable String name) {
        Map<String, Object> map = new HashMap<>();
        //2.0.3版本取值有问题，1.5.3版本正常
        map.put(name, environment.getProperty(name));
        return map;
    }

    @GetMapping("/wechat-appid")
    public String getWeChatAppId() {
        return wechatAppId;
    }
}
